#include "mybutton.h"
#include <QPainter>

MyButtonCustom::MyButtonCustom(QWidget *parent)
    : QPushButton(parent), m_color(Qt::blue)
{
    setText("My Button");
}

MyButtonCustom::MyButtonCustom(const QString &text, QWidget *parent)
    : QPushButton(text, parent), m_color(Qt::blue)
{
}

QColor MyButtonCustom::color() const
{
    return m_color;
}

void MyButtonCustom::setColor(const QColor &color)
{
    if (m_color != color) {
        m_color = color;
        update();
    }
}

void MyButtonCustom::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制颜色预览
    QRect colorRect(5, 5, 20, 20);
    painter.setBrush(m_color);
    painter.setPen(Qt::black);
    painter.drawRect(colorRect);
}
